﻿public class Sorter {

    public void traversal( int[] array ){
        if( array != null && array.length > 0 ) {
            for( int i = 0 ; i < array.length ; i++ ){
                System.out.print( array[ i ] );
                System.out.print( ( i < array.length - 1 ) ? " , " : "" );
            }
            System.out.println();
        }
    }

    public int[] sort( int[] array ) {
        // 实现排序并返回一个新数组
      int [] newArray = new int[array.length];
      for (int i = 0;i < newArray.length - 1;i++){
          newArray[i] = array[i];
      }
      for (int i = 0;i < newArray.length - 1;i++){
          for (int j = 0; j < newArray.length - 1 -i; j++) {
              if (newArray[j] > newArray[j + 1]){
                  newArray[j] ^= newArray[j + 1];
                  newArray[j + 1] ^= newArray[j];
                  newArray[j] ^= newArray[j + 1];
              }
          }
      }
      return newArray;
    }

    public static void main(String[] args) {
        Sorter sorter = new Sorter();

        int[] a = { 1 , 100 , -20 , 99 , 1000 , 0 , 30 };
        System.out.print( "原数组: " );
        sorter.traversal( a ); // 原数组: 1 , 100 , -20 , 99 , 1000 , 0 , 30

        int[] x = sorter.sort( a ) ;
        System.out.print( "新数组: " );
        sorter.traversal( x ); // 新数组: -20 , 0 , 1 , 30 , 99 , 100 , 1000

        System.out.print( "原数组: " );
        sorter.traversal( a ); // 原数组: 1 , 100 , -20 , 99 , 1000 , 0 , 30
    }

}
